package com.glg.springmvcdemo2;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@RequestMapping("/param")
@RestController
public class UserController {

    @RequestMapping("/m1")
    public String method1(String name) {
        return "接收到参数：name="+name;
    }

    @RequestMapping("/m1/int")
    public String method1GetInt(Integer age) {
        return "接收到参数：age="+age;
    }

    @RequestMapping("/m2")
    public String method2(String name, String password) {
        return "接收到参数：name=" + name + ", password=" + password;
    }

    @RequestMapping("/m3")
    public Object method3(Person person) {
        return person.toString();
    }

    @RequestMapping("/m4")
    public Object method4(@RequestParam("time") String createtime) {
        return "收到参数：createtime="+ createtime;
    }

    @RequestMapping("/m5")
    public Object method5(@RequestParam(value = "time", required = false) String createtime) {
        return "接收到参数 createtime: " + createtime;
    }

    @RequestMapping("/m6")
    public String method6(String[] arrayParam) {
        return Arrays.toString(arrayParam);
    }

    @RequestMapping("/m7")
    public String method7(@RequestParam List<String> listParam) {
        return "size: " + listParam.size() + ", listParam:" + listParam;
    }

    @RequestMapping("/m8")
    public Object method8(@RequestBody Person person){
        return person.toString();
    }

    @RequestMapping("/m9")
    public Object method9(Person person){
        return person.toString();
    }

    @RequestMapping("/m10/{id}/{name}")
    public Object method10(@PathVariable Integer id, @PathVariable("name") String userName){
        return "解析参数id: "+id+", name: "+userName;
    }

    @RequestMapping("/m11")
    public Object method11(@PathVariable("file")MultipartFile file) throws IOException {
        //获取文件名称
        String fileName = file.getOriginalFilename();
        //文件上传到指定路径
        file.transferTo(new File("D:/temp/" + file.getOriginalFilename()));
        return "接收到文件名称为: "+fileName;
    }

}
